package wan.android.photo;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;

import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

import java.io.File;

public class ImageLoaderHelper {
	private Context context;

	public ImageLoaderHelper(Context context) {
		this.context = context;
	}
	public final static DisplayImageOptions options = new DisplayImageOptions.Builder()
			.showImageForEmptyUri(R.mipmap.ic_launcher)
			.showImageOnLoading(R.mipmap.ic_launcher)
			.showImageForEmptyUri(R.mipmap.ic_launcher)
			.showImageOnFail(R.mipmap.ic_launcher)
			.cacheInMemory(true)
			.cacheOnDisk(true)
			.considerExifParams(true)
			.bitmapConfig(Bitmap.Config.RGB_565)
			.displayer(new FadeInBitmapDisplayer(388)).build();
	public void init(String folder) {
		initImageLoader(folder);
		DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder() //
				.showImageForEmptyUri(R.mipmap.ic_launcher) //
				.showImageOnFail(R.mipmap.ic_launcher) //
				.cacheInMemory(true) //
				.cacheOnDisk(true) //
				.build();//
		ImageLoaderConfiguration config = new ImageLoaderConfiguration//
		.Builder(context)//
				.defaultDisplayImageOptions(defaultOptions)//
				.diskCacheSize(50 * 1024 * 1024)//
				.diskCacheFileCount(100)// 
				.writeDebugLogs()//
				.build();//
		ImageLoader.getInstance().init(config);
		
	}
	private void initImageLoader(String folder) {

		File root=context.getExternalCacheDir();
		File photoCache=new File(root,folder);
		if (!photoCache.exists())
			photoCache.mkdirs();
		Log.e("test","缓存目录:"+photoCache.getAbsolutePath());
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
				.threadPriority(Thread.MAX_PRIORITY)
				.denyCacheImageMultipleSizesInMemory()
				.diskCache(new UnlimitedDiscCache(photoCache))
				.diskCacheFileNameGenerator(new Md5FileNameGenerator())
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				.writeDebugLogs() // Remove for release app
				.build();
		ImageLoader.getInstance().init(config);
	}
}
